import 'package:get/get.dart';
// import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/models/common/MessageProperty.dart';
import 'package:ichinesereader/models/oauth/linkRequest.dart';
import 'package:ichinesereader/models/oauth/linkResut.dart';
import 'package:ichinesereader/service/oauth.dart';
import 'package:ichinesereader/utils/helper.dart';

class OauthController extends GetxController with StateMixin<dynamic> {
  OauthService service = new OauthService();
  Rx<LinkResult> _linkResult =
      Rx<LinkResult>(LinkResult(uid: '', token: '', role: '', email: ''));
  Rx<LinkResult> get linkResult => this._linkResult;

  void callClasslink(String bearcode) async {
    try {
      var response = await service.callClasslink(bearcode);
      if (response.statusCode == 200) {
        var data = linkResultFromJson(response.bodyString);
        _linkResult.value = data;
        Get.to('/xlogin');
      } else {
        MessagePropertyResult mp = messagePropertyResultFromJson(response.body);
        showSnackMsg(mp.message!.message!);
      }
    } catch (error) {
      print(error);
    }
  }

  void doLink(LinkRequest request) async {
    try {
      var response = await service.doLink(request);
      if (response.statusCode == 200) {
        var data = linkResultFromJson(response.bodyString);
        _linkResult.value = data;
        Get.to('/xlogin');
      } else {
        MessagePropertyResult mp = messagePropertyResultFromJson(response.body);
        showSnackMsg(mp.message!.message!);
      }
    } catch (error) {
      print(error);
    }
  }
}
